﻿var FormatData = {
    formatDate: function (value) {
        if (value == null) {
            return "--";
        } else {
            if (value.indexOf('T') > 0) {
                var dateT = value.replace('T', ' ');
                return dateT.substring(0, 19);
            } else {
                var date = new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
                //月份为0-11，所以+1，月份小于10时补个0
                var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
            }

        }
    },
    formatBool: function (value) {
        if (value == "1") {
            return "启用";
        } else {
            return "禁用";
        }
    },
    formatValid: function (value) {
        if (value == "1") {
            return "有效";
        } else {
            return "无效";
        }
    },
    formatGoodsStatus: function (value) {
        if (value == "0") {
            return "新品";
        }
        if (value == "1") {
            return "稳定品";
        }
        if (value == "2") {
            return "暂停进货";
        }
        if (value == "3") {
            return "暂停运作";
        }
        if (value == "4") {
            return "暂停要货";
        }
        if (value == "8") {
            return "清场品";
        }
        if (value == "9") {
            return "历史品";
        } else {
            return "暂缺状态";
        }
    },
    formatModuleType: function (value) {
        if (value == null || value === "undefined" || value != "") {
            value = value == "2" ? "IOS" : value == "3" ? "Android" : "Web";
        } else {
            value = "Web";
        }
        return value;
    },
    toString: function (value) {
        if (value == null || value === "undefined") {
            value = "";
        } else {
            value = value.toString();
        }
        return value;
    }, formatvalidSumQty: function (value) {
        if (value != null) {
            if (value.PURQTY != null && value.PURQTY != "") {
                var purQty = value.PURQTY;
                return purQty + " " + value.PURUNITNAME;
            } else {
                return "";
            }

        }
    }, formatvalidDay: function (value) {
        var productiondate = value.PRODUCTIONDATE;
        var productionyear = 201 + productiondate.substr(0, 1);
        var productionmonth = productiondate.substr(1, 2);
        var productionday = productiondate.substr(3, 2);
        var strProduction = productionyear + '-' + productionmonth + '-' + productionday;
        strProduction = strProduction.replace(/-/g, "/");
        var dateProduction = new Date(strProduction);

        var date = new Date();
        var year = date.getFullYear().toString();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var strnowdate = year + '-' + month + '-' + day;
        strnowdate = strnowdate.replace(/-/g, "/");
        var nowdate = new Date(strnowdate);
        var pastdate = parseInt((nowdate - dateProduction) / 1000 / 60 / 60 / 24);
        var expirydays = value.EXPIRYDAYS;
        return expirydays - pastdate;


    }, formatflag: function (value) {
        if (value != null) {
            if (value == 0) {
                return "未处理";
            }
            if (value == 40) {
                return "在执行";
            }
            if (value == 100) {
                return "完结";
            }
        }
    },
    formatQualityStatus: function (value) {
        if (value == "1") {
            return "临保";
        } else {
            return "有效";
        }
    },
    formatJDOrderStatus: function (value) {
        if (value == "0") {
            return "已下架";
        }
        else if (value == "1") {
            return "未下架";
        }
        else {
            return "已取消";
        }
    },
    formatOrderDisStatus: function (value) {
        if (value == "0" || value=="") {
            return "未配送";
        }
        else if (value == "1") {
            return "配送中";
        }
        else if (value == "2") {
            return "已完成";
        }
    },
    formatOrderDisType: function (value) {
        if (value == "0" || value == "") {
            return "未分配";
        }
        else if (value == "1") {
            return "我厨";
        }
        else if (value == "2") {
            return "达达";
        }
    }
};


var HandleDialog = {
    OpenDialogByAdd: function (controlId, title, callback) {
        $('#' + controlId).dialog('open').dialog('setTitle', title);
        if (callback != undefined) {
            callback();
        }
    },
    CloseDialog: function (controlId) {
        $('#' + controlId).dialog('close');
    },
    OpenDialog: function (rowData, controlId, title, callback) {
        if (rowData) {
            $('#' + controlId).dialog('open').dialog('setTitle', title);
            if (callback != undefined) {
                callback();
            }
        } else {
            HandleTips.Alert('请先选中一条数据，再进行相关操作！');
            //$('#dlg').dialog('close');
        }
    },
    ShowDailog: function (controlId, title,left,top, width, height, closed, model,url)
    {
        if (!closed) closed = true;
        if (!model) model = true;

        $('#' + controlId).dialog({
            left:left,
            top:top,
            title: title,
            width: width,
            height: height,
            closed: closed,            
            modal: model,
            href: url,
        }).dialog('open');
    }
};

var HandleMenu = {
    ShowMenu: function (menuId, e) {
        $('#' + menuId).menu('show', {
            left: e.pageX,
            top: e.pageY
        });
    },
    CloseDialog: function (controlId) {
        $('#' + controlId).dialog('close');
    }
};

$.extend($.fn.validatebox.defaults.rules, {
    /*必须和某个字段相等*/
    equalTo: {
        validator: function (value, param) {
            return $(param[0]).val() === value;
        },
        message: '字段不匹配'
    },
    compareDate: {
        validator: function (value, param) {
            var startDate = $(param[0]).datebox('getValue');
            startDate = $.fn.datebox.defaults.parser(startDate);
            var endDate = $.fn.datebox.defaults.parser(value);
            return endDate >= startDate;
        },
        message: '请正确填写日期范围'
    },
    minLength: { // 判断最小长度
        validator: function (value, param) {
            return value.length >= param[0];
        },
        message: '最少输入 {0} 个字符。'
    },
    length: {
        validator: function (value, param) {
            var len = $.trim(value).length;
            return len >= param[0] && len <= param[1];
        },
        message: "内容长度介于{0}和{1}之间."
    },
    phone: {// 验证电话号码
        validator: function (value) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '格式不正确,请使用下面格式:020-88888888'
    },
    mobile: {// 验证手机号码
        validator: function (value) {
            return /^(13|15|18)\d{9}$/i.test(value);
        },
        message: '手机号码格式不正确(正确格式如：13450774432)'
    },
    phoneOrMobile: {//验证手机或电话
        validator: function (value) {
            return /^(13|15|18)\d{9}$/i.test(value) || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '请填入手机或电话号码,如13688888888或020-8888888'
    },
    idcard: {// 验证身份证
        validator: function (value) {
            return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
        },
        message: '身份证号码格式不正确'
    },
    floatOrInt: {// 验证是否为小数或整数
        validator: function (value) {
            return /^(\d{1,3}(,\d\d\d)*(\.\d{1,3}(,\d\d\d)*)?|\d+(\.\d+))?$/i.test(value);
        },
        message: '请输入数字，并保证格式正确'
    },
    currency: {// 验证货币
        validator: function (value) {
            return /^d{0,}(\.\d+)?$/i.test(value);
        },
        message: '货币格式不正确'
    },
    qq: {// 验证QQ,从10000开始
        validator: function (value) {
            return /^[1-9]\d{4,9}$/i.test(value);
        },
        message: 'QQ号码格式不正确(正确如：453384319)'
    },
    integer: {// 验证整数
        validator: function (value) {
            return /^[+]?[1-9]+\d*$/i.test(value);
        },
        message: '请输入整数'
    },
    chinese: {// 验证中文
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/i.test(value);
        },
        message: '请输入中文'
    },
    english: {// 验证英语
        validator: function (value) {
            return /^[A-Za-z]+$/i.test(value);
        },
        message: '请输入英文'
    },
    unnormal: {// 验证是否包含空格和非法字符
        validator: function (value) {
            return /^[a-zA-Z0-9\u0391-\uFFE5_]+$/i.test(value);
        },
        message: '输入值不能为空和包含其他非法字符'
    },
    username: {// 验证用户名
        validator: function (value) {
            return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
        },
        message: '用户名不合法（字母开头，允许6-16字节，允许字母数字下划线）'
    },
    faxno: {// 验证传真
        validator: function (value) {
            //			return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value);
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: '传真号码不正确'
    },
    zip: {// 验证邮政编码
        validator: function (value) {
            return /^[1-9]\d{5}$/i.test(value);
        },
        message: '邮政编码格式不正确'
    },
    ip: {// 验证IP地址
        validator: function (value) {
            return /d+.d+.d+.d+/i.test(value);
        },
        message: 'IP地址格式不正确'
    },
    name: {// 验证姓名，可以是中文或英文
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
        },
        message: '请输入姓名'
    },
    carNo: {
        validator: function (value) {
            return /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/.test(value);
        },
        message: '车牌号码无效（例：粤J12350）'
    },
    carenergin: {
        validator: function (value) {
            return /^[a-zA-Z0-9]{16}$/.test(value);
        },
        message: '发动机型号无效(例：FG6H012345654584)'
    },
    email: {
        validator: function (value) {
            return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
        },
        message: '请输入有效的电子邮件账号(例：abc@126.com)'
    },
    msn: {
        validator: function (value) {
            return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
        },
        message: '请输入有效的msn账号(例：abc@hotnail(msn/live).com)'
    }, department: {
        validator: function (value) {
            return /^[0-9]*$/.test(value);
        },
        message: '请输入部门排序号(例：1)'
    }, same: {
        validator: function (value, param) {
            if ($("#" + param[0]).val() != "" && value != "") {
                return $("#" + param[0]).val() == value;
            } else {
                return true;
            }
        },
        message: '两次输入的密码不一致！'
    }, forIntToOneHundred: {// 验证0-100整数
        validator: function (value) {
            return /^([0-9]{1,2}|100)$/.test(value);
        },
        message: '两次输入的密码不一致！'
    },
    isChanged: {
        validator: function (value, param)
        {
            var value_old = $("#"+param[0]).attr("value_old");
            if(value!=value_old)
            {
              //  var url = param[1];
              //  var dataId = param[2];
              //  var checkFun = param[3];
              //  var data = { dataId: $("#" + dataId).val() };
              ////  $.ajaxPost(url, data, checkFun);
              //  $.ajax({
              //      type: "post",         //请求类型,get post
              //      async: false,
              //      url: url,
              //      data: data,             //传入参数
              //     // dataType: "json",    //返回类型
              //      success: function (response)
              //      {
              //          if (response == 0)
              //              return true;
              //      }
              //  });
                return $("#" + param[1]).val()=="true";
            }
            else
                return true;
        },
        message: '{2}'
    },
    isCompared: {
        validator: function (value, param)
        {
            var val1 = $("#" + param[1]).val();
            var val2 = $("#" + param[2]).val();
            if (val1 == val2)
            {                
                return value == param[3];
            }
            else
                return true;
        },
        message: '{0}'
    }
});
